<template>
  <div class="home">
    <p class="home-title">Todo List</p>
    <div class="home-body">
      <todo-input @onChange="handleChange"></todo-input>
      <todo-list @onDel="handleDel" @onChecked="handleChecked"></todo-list>
      <todo-total @onCleanCompleted="handleCleanCompleted"></todo-total>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import TodoInput from '@/components/TodoInput'
import TodoList from '@/components/TodoList'
import TodoTotal from '@/components/TodoTotal'
import { useConfig } from '@/utils'
export default {
  name: 'Home',
  components: {
    TodoInput,
    TodoList,
    TodoTotal
  },
  setup() {
    const config = useConfig()
    const store = useStore()
    const list = store.state.list
    const handleChange = (value) => {
      const index = list.findIndex(o => o.title === value)
      if (index === -1) {
        const id = list.length ? list[list.length - 1].id + 1 : 0
        store.commit('addTodo', {
          id,
          title: value,
          isCompleted: false
        })
      } else {
        config.$message({
          message: '任务已存在',
          type: 'warning',
        })
      }
    }
    const handleDel = (index) => {
      store.commit('deleteTodo', index)
    }
    const handleChecked = (index) => {
      store.commit('checkedTodo', index)
    }
    const handleCleanCompleted = (complete) => {
      store.commit('cleanCompleted', complete)
    }
    return {
      handleChange,
      handleDel,
      handleChecked,
      handleCleanCompleted
    }
  }
}
</script>
<style scoped lang="scss">
.home {
  width: 320px;
  margin: 100px auto;
  border: 1px solid #e5e5e5;
  border-radius: 4px;
  &-title {
    font-size: 20px;
    font-weight: bold;
    line-height: 40px;
    text-align: center;
    background-color: #e5e5e5;
  }
  &-body {
    padding: 16px;
  }
}
</style>
